#include "basepersent.h"
#include "utils/deviceutils.h"
#include "bean/stringboxbean.h"
BasePersent::BasePersent(QWidget *parent) : QObject()
{
    this->widget = parent;
}
/**
 * @brief GamePersent::checkHell 判断是否死亡
 * @return true 是地府 false 不是地府
 */
void BasePersent::checkDeal(CallBack *fun)
{
    QTimer::singleShot(500, this, [=](){
        bool bloodEmpty = (getBlood() == 0);
        qDebug() << "血量1" <<getBlood();
        if(bloodEmpty){
            QTimer::singleShot(1500, this, [=](){
                bool bloodEmpty = (getBlood() == 0);
                qDebug()<< "血量2" << getBlood();
                if(bloodEmpty){
                    GameManager::getInstantce()->roleLocation = ROLE_LOCATION_REND;
                    DeviceUtils::sendLog(widget, "检查到你已死亡，准备复活");
                    fun->callBack();
                }
            });
        }
    });

    bool df = getMapString().contains("siwang");
    if(df){
        GameManager::getInstantce()->roleLocation = ROLE_LOCATION_HELL;
        DeviceUtils::sendLog(widget,"你当前在地府，准备出地府");
        fun->callBack();
    }
}

int BasePersent::getMagic()
{
    QRect r= ImgHelper::getInstance()->getRotatedRect(Scalar(119, 66, 255), Scalar(122, 163, 255), QRect(60,40,165,65));
    return r.width() * 100 / BLOOD_MAX_VALUE;
}



//获取血量 百分比，例如64%的血量返回的是64
int BasePersent::getBlood()
{
    QRect r= ImgHelper::getInstance()->getRotatedRect(Scalar(0, 138, 198), Scalar(8, 255, 255), QRect(60,40,165,65));
    return r.width() * 100 / BLOOD_MAX_VALUE;
}

int BasePersent::getFoeBlood()
{
    QRect r= ImgHelper::getInstance()->getRotatedRect(Scalar(0, 138, 198), Scalar(8, 255, 255), QRect(250,40,165,65));
    return r.width() * 100 / BLOOD_MAX_VALUE;
}


//获取当前所在地图名称 如无量山返回的是 wuliang， 选人界面返回的是xuanren
QString BasePersent::getMapString()
{
    char map[17];
    if(GameManager::getInstantce()->hProcess == nullptr || GameManager::getInstantce()->baseAddress == 0){
        return QString();
    }
    DWORD addressX = Win32Util::getTargetAddress(GameManager::getInstantce()->hProcess, GameManager::getInstantce()->baseAddress, 4, 0x003B9D88 , 0x2C, 0x70, 0xc4);
    ReadProcessMemory(GameManager::getInstantce()->hProcess, (DWORD*)addressX, map,sizeof(map),0);

    QString mapStr = QString::fromStdString(map);
    if(mapStr.contains('.')){
       mapStr = mapStr.section(".",0,0);
    }

    return mapStr.replace("_SP","");
}


QPoint BasePersent::getRolesPoint()
{
    StringBoxBean stringBoxs;
    QPoint point;
    ;
    QRect locatRect = QRect(GameManager::getInstantce()->gameRect.right() - 160, 20, 100,80);
    ImgHelper::getNumByImgAbs(Scalar(28, 138, 255), Scalar(31, 141, 255), locatRect,stringBoxs, RIL_TEXTLINE);
    QString ponitStr = stringBoxs.word.replace("\n","");
    QStringList list = ponitStr.split(" ");
    if(list.size() == 2){
        bool isOk;

        int x = list.at(0).toInt(&isOk);
        if(!isOk) return point;
        int y = list.at(1).toInt(&isOk);
        if(!isOk) return point;
        point.setX(x);
        point.setY(y);
    }
    return point;
}
